package ae.pcfc.etks.online.web.controller.user.general.request.view;

/* $Id: RegisterController 
 * Copyright (C) 2011, Trakhees
 *  
 *  History
 *
 * Date  			Author 					Remarks
 * -------------------------------------------------------------
 * April 10, 2011 	Mohtisham.Salahria      Created
 *
 */

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import ae.pcfc.etks.online.common.delegate.AppMetaDataDelegate;
import ae.pcfc.etks.online.common.delegate.ClientRegistrationDelegate;
import ae.pcfc.etks.online.common.delegate.ServiceDelegate;
import ae.pcfc.etks.online.common.delegate.UserDelegate;
import ae.pcfc.etks.online.common.vo.AppMetaDataDetailVO;
import ae.pcfc.etks.online.common.vo.ClientRoleVO;
import ae.pcfc.etks.online.common.vo.ServiceVO;
import ae.pcfc.etks.online.web.ConstantsAdmin;
import ae.pcfc.etks.online.web.ViewPath;
import ae.pcfc.etks.online.web.controller.BaseController;
import ae.pcfc.etks.online.web.controller.user.general.home.HomeController;
import ae.pcfc.etks.online.web.model.Service;
import ae.pcfc.etks.online.web.model.UserRequestModel;
import ae.pcfc.etks.online.web.util.SecurityUtils;

/**
 * Controller that handles all {@link HomeController}-related requests, i.e.:
 * all requests for /user/general/home/*
 * 
 * @author Mohtisham.Salahria
 */
@Controller
@RequestMapping(ViewPath.USER_GENERAL_REQUEST_VIEW)
public class RequestView extends BaseController{

	private UserDelegate userDelegate;

	@Resource(name = "userDelegate")
	public void setUserDelegate(UserDelegate userDelegate) {
		this.userDelegate = userDelegate;
	}

	private ServiceDelegate serviceDelegate;

	@Resource(name = "serviceDelegate")
	public void setServiceDelegate(ServiceDelegate serviceDelegate) {
		this.serviceDelegate = serviceDelegate;
	}

	private AppMetaDataDelegate appMetaDataDelegate;

	@Resource(name = "appMetaDataDelegate")
	public void setAppMetaDataDelegate(AppMetaDataDelegate appMetaDataDelegate) {
		this.appMetaDataDelegate = appMetaDataDelegate;
	}

	private ClientRegistrationDelegate clientRegistrationDelegate;

	@Resource(name = "clientRegistrationDelegate")
	public void setClientRegistrationDelegate(ClientRegistrationDelegate clientRegistrationDelegate) {
		this.clientRegistrationDelegate = clientRegistrationDelegate;
	}

	/**
	 * Custom handler for displaying the form, i.e.: GET requests for
	 * /client/register/
	 * 
	 * @see WEB-INF/jsp/user/general/view/request/show.jsp
	 */

	@RequestMapping(value = "/show.htm", method = RequestMethod.GET)
	public void show(@RequestParam(value = "refNo", required = false) String refNo,
			@ModelAttribute(value = ConstantsAdmin.USER_REQUEST_VIEW) UserRequestModel userRequestModel, Model model) {

		AppMetaDataDetailVO appMetaDataVO = this.appMetaDataDelegate.loadAppMetaDataByClientRefNo(SecurityUtils.decryptQueryParam(refNo));

		if (appMetaDataVO != null) {
			
			getMapper().map(appMetaDataVO, userRequestModel);
			
/*			if(appMetaDataVO.getCommentsVOs() != null && appMetaDataVO.getCommentsVOs().size() > 0){
				userRequestModel.setStrUserComments(appMetaDataVO.getCommentsVOs().get(0).getStrCommentsUser());
			}
*/			
			if (appMetaDataVO.getStrApplicationId() != null) {
				List<ServiceVO> serviceVOs = this.serviceDelegate.findServiceRequestByApplicationId(appMetaDataVO.getStrApplicationId());

				if (serviceVOs != null && serviceVOs.size() > 0) {
					List<Service> serviceModels = new ArrayList<Service>();
					Service service;
					for (ServiceVO serviceVO : serviceVOs) {
						service = new Service();
						getMapper().map(serviceVO, service);
						serviceModels.add(service);
					}

					model.addAttribute("serviceRequest", serviceModels);
				}

				List<ClientRoleVO> clientRoleVOs = this.clientRegistrationDelegate.findClientReqRoleByApplicationId(appMetaDataVO.getStrApplicationId());

				if (clientRoleVOs != null && clientRoleVOs.size() > 0) {
					model.addAttribute("clientRoleList", clientRoleVOs);
				}
			}
		}
	}

}
